﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace TmU220PrintHelperV2d0.Entity
{
    /// <summary>
    /// Esc命令常数
    /// </summary>
    public class PrinterEscCommandConstantV2d0
    {

        public const byte ESC = 27;    // 换码
        public const byte FS = 28;    // 文本分隔符
        public const byte GS = 29;    // 组分隔符
        public const byte DLE = 16;    // 数据连接换码
        public const byte EOT = 4;    // 传输结束
        public const byte ENQ = 5;    // 询问字符
        public const byte SP = 32;    // 空格
        public const byte HT = 9;    // 横向列表
        public const byte LF = 10;    // 打印并换行（水平定位）
        public const byte CR = 13;    // 归位键
        public const byte FF = 12;    // 走纸控制（打印并回到标准模式（在页模式下） ）
        public const byte CAN = 24;    // 作废（页模式下取消打印数据 ）
        /**
         * 打印纸一行最大的字节
         */
        private const int LINE_BYTE_SIZE = 32;
        /**
         * 分隔符
         */
        private const String SEPARATOR = "$";
        private static StringBuilder sb = new StringBuilder();

        /**
         * 打印机初始化
         * 
         * @return
         */
        public static byte[] init_printer()
        {
            byte[] result = new byte[2];
            result[0] = ESC;
            result[1] = 64;
            return result;
        }
        /**
         * 打开钱箱
         * 
         * @return
         */
        public static byte[] open_money()
        {
            byte[] result = new byte[5];
            result[0] = ESC;
            result[1] = 112;
            result[2] = 48;
            result[3] = 64;
            result[4] = 0;
            return result;
        }

        /**
         * 换行
         * 
         * @param lineNum要换几行
         * @return
         */
        public static byte[] nextLine(int lineNum)
        {
            byte[] result = new byte[lineNum];
            for (int i = 0; i < lineNum; i++)
            {
                result[i] = LF;
            }

            return result;
        }


        /**
         * 绘制下划线（1点宽）
         * 
         * @return
         */
        public static byte[] underlineWithOneDotWidthOn()
        {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 45;
            result[2] = 1;
            return result;
        }

        /**
         * 绘制下划线（2点宽）
         * 
         * @return
         */
        public static byte[] underlineWithTwoDotWidthOn()
        {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 45;
            result[2] = 2;
            return result;
        }

        /**
         * 取消绘制下划线
         * 
         * @return
         */
        public static byte[] underlineOff()
        {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 45;
            result[2] = 0;
            return result;
        }


        /**
         * 选择加粗模式
         * 
         * @return
         */
        public static byte[] boldOn()
        {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 69;
            result[2] = 0xF;
            return result;
        }

        /**
         * 取消加粗模式
         * 
         * @return
         */
        public static byte[] boldOff()
        {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 69;
            result[2] = 0;
            return result;
        }


        /**
         * 左对齐
         * 
         * @return
         */
        public static byte[] alignLeft()
        {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 97;
            result[2] = 0;
            return result;
        }

        /**
         * 居中对齐
         * 
         * @return
         */
        public static byte[] alignCenter()
        {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 97;
            result[2] = 1;
            return result;
        }

        /**
         * 右对齐
         * 
         * @return
         */
        public static byte[] alignRight()
        {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 97;
            result[2] = 2;
            return result;
        }

        /// <summary>
        /// 传送打印机打印纸传感器状态
        /// </summary>
        /// <returns></returns>
        public static byte[] transferPaperSensorStatus()
        {
            byte[] result = new byte[3];
            result[0] = GS;
            result[1] = 114;
            result[2] = 1;
            return result;
        }

        /// <summary>
        /// 允许自动状态回复(ASB)
        /// 允许联机/脱机状态。2
        /// 允许错误状态。4
        /// 允许卷纸传感器状态。8
        /// </summary>
        /// <returns></returns>
        public static byte[] enableAsb()
        {
            byte[] result = new byte[3];
            result[0] = GS;
            result[1] = 97;
            result[2] = 2 + 4 + 8;
            return result;
        }



        /// <summary>
        /// 打印并进纸若干个垂直运动单位
        /// </summary>
        /// <param name="col">几个行间距</param>
        /// <returns></returns>
        public static byte[] printAndFeedPaper_VerticalUnit(byte col)
        {
            byte[] result = new byte[4];
            result[0] = ESC;
            result[1] = 74;//100
            result[2] = col;
            return result;
        }
        /// <summary>
        /// 打印并进纸若干个行间距
        /// </summary>
        /// <param name="col">几个行间距</param>
        /// <returns></returns>
        public static byte[] printAndFeedPaper_VertialHjj(byte col)
        {
            byte[] result = new byte[4];
            result[0] = ESC;
            result[1] = 74;//100
            result[2] = col;
            return result;
        }
        /**
         * 字体变大为标准的n倍
         * 
         * @param num
         * @return
         */
        public static byte[] fontSizeSetBig(int num)
        {
            byte realSize = 0;
            switch (num)
            {
                case 1:
                    realSize = 0;
                    break;
                case 2:
                    realSize = 17;
                    break;
                case 3:
                    realSize = 34;
                    break;
                case 4:
                    realSize = 51;
                    break;
                case 5:
                    realSize = 68;
                    break;
                case 6:
                    realSize = 85;
                    break;
                case 7:
                    realSize = 102;
                    break;
                case 8:
                    realSize = 119;
                    break;
            }
            byte[] result = new byte[3];
            result[0] = 29;
            result[1] = 33;
            result[2] = realSize;
            return result;
        }


        /// <summary>
        /// 设置字符字体打印模式
        //0 选择字符字体A (9 × 9
        //1 选择字符字体B(7 × 9
        //0 保留
        //0 没有选择粗体模式。
        //8 选择粗体模式。
        //0 没有选择倍高模式。
        //16 选择倍高模式。
        //0 没有选择倍宽模式。
        //32 选择倍宽模式。
        //0 保留
        //0 没有选择下划线模式。
        //128 选择下划线模式
        /// </summary>
        /// <returns></returns>
        public static byte[] AscFontPrintMode(byte mode)
        {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 33;
            result[2] = mode;
            return result;
        }
        /// <summary>
        /// 设置多字节编码字符的打印模式
        //0 没有选择倍宽模式。
        //4 选择倍宽模式。
        //0 没有选择倍高模式。
        //8 选择倍高模式。
        //0 保留
        //0 没有选择多字节编码下划
        //128 选择多字节编码下划线。
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static byte[] MbcsFontPrintMode(byte mode)
        {
            byte[] result = new byte[3];
            result[0] = FS;
            result[1] = 33;
            result[2] = mode;
            return result;
        }

        /// <summary>
        /// 进纸并全部切割
        /// </summary>
        /// <returns></returns>
        public static byte[] feedPaperCutAll()
        {
            byte[] result = new byte[4];
            result[0] = GS;
            result[1] = 86;
            result[2] = 65;
            result[3] = 0;
            return result;
        }

        /// <summary>
        /// 进纸并切割（左边留一点不切）
        /// </summary>
        /// <returns></returns>
        public static byte[] feedPaperCutPartial()
        {
            byte[] result = new byte[4];
            result[0] = GS;
            result[1] = 86;
            result[2] = 66;
            result[3] = 0;
            return result;
        }
        /// <summary>
        /// 每次只能打印四行
        /// </summary>
        /// <param name="m"></param>
        /// <param name="nL">0-255圖像的像素列個數（最多256列）</param>
        /// <param name="nH">0,1,2,3圖像的像素行個數（最多4行）</param>
        /// <returns></returns>
        public static byte[] bitMapPrintHead(byte m, byte nL, byte nH)
        {
            byte[] result = new byte[5 + 256 * nH];
            result[0] = ESC;
            result[1] = 42;
            result[2] = m;
            result[3] = (byte)255;
            result[4] = (byte)(nH - 1);
            return result;
        }
        /// <summary>
        /// 缺省行间距
        /// </summary>
        /// <returns></returns>
        public static byte[] defaultVerticalSpace()
        {
            byte[] result = new byte[2];
            result[0] = ESC;
            result[1] = 50;
            return result;
        }

        /// <summary>
        /// 图像打印行间距 n=24 1/6 英寸 默认值
        /// </summary>
        /// <returns></returns>
        public static byte[] bitMapVerticalSpace(byte n)
        {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = 3;
            result[2] = n;
            return result;
        }

        /// <summary>
        /// 打印颜色
        /// </summary>
        /// <param name="color">颜色，默认Black,红色Red</param>
        /// <returns></returns>
        public static byte[] fontColor(string color="Black")
        {
            byte[] result = new byte[3];            
            if (color=="Black")
            {
                result[0] = ESC;
                result[1] = 114;
                result[2] = 0;
            }
            else if (color == "Red")
            {
                result[0] = ESC;
                result[1] = 114;
                result[2] = 1;
            }
            return result;
        }

        /// <summary>
        /// 实时状态传送
        /// </summary>
        /// <param name="n">n:1 实时传输打印机状态，n:2 传送打印机脱机状态，n:3 传送错误状态，n:4 传送卷纸传感器状态</param>
        /// <returns></returns>
        public static byte[] transferPrinterStatus(byte n)
        {
            byte[] result = new byte[3];
            result[0] = ESC;
            result[1] = EOT;
            result[2] = n;
            return result;
        }

        public static byte[] horizontalPosition()
        {
            byte[] result = new byte[8];
            result[0] = ESC;
            result[1] = 68;
            result[3] = 16;
            result[4] = 32;
            result[5] = 48;
            result[6] = 64;
            result[7] = 0;
            return result;
        }
        /**
 * 水平方向向右移动col列
 * 
 * @param col
 * @return
 */
        public static byte[] set_HT_position(byte col)
        {
            byte[] result = new byte[4];
            result[0] = ESC;
            result[1] = 36;
            result[2] = col;
            result[3] = 0;
            return result;
        }
        public static byte[] set_HT()
        {
            byte[] result = new byte[1];
            result[0] = 9;
            return result;
        }





    }
}
